//
//  Net.m
//
//  Created by SKY
//  Copyright © 翊sky. All rights reserved.
//

#import "Net.h"
#import "AFNet.h"
#import "AppDelegate.h"
#import "NetProgressView.h"
/**
 * If you are not satisfied, you can continue to package
 */
@interface Net ()

@end

static UIView *alertView = nil;
static NetProgressView *netProgressView = nil;

@implementation Net

+ (void)getRequestHttpURL:(NSString *)url
                      view:(UIView *)view
              completation:(SuccessBlock)success {
    
    [self setNetworkActivityIndicator:YES];
    [self showProgressView:view];
    
    [AFNet getRequestHttpURL:url completation:^(id object) {
        
        if (success) {
            success(object);
        }
        
        [self setNetworkActivityIndicator:NO];
        [self closeProgressViewFromView:view];
        
    } failure:^(NSError *error) {

        [self setNetworkActivityIndicator:NO];
        [self closeProgressViewFromView:view];
        
        [self errorView:view];

    }];
    
}
+ (void)postRequestHttpURL:(NSString *)url
                       view:(UIView *)view
                  parameter:(id)parameter
               completation:(SuccessBlock)success {
    
    [self setNetworkActivityIndicator:YES];
    [self showProgressView:view];
    
    [AFNet postRequestHttpURL:url parameter:parameter completation:^(id object) {
        
        if (success) {
            success(object);
        }
        
        [self setNetworkActivityIndicator:NO];
        [self closeProgressViewFromView:view];

    } failure:^(NSError *error) {
        
        [self setNetworkActivityIndicator:NO];
        [self closeProgressViewFromView:view];
        
        [self errorView:view];
        
    }];
    
}

+ (void)postUploadHttpURL:(NSString *)url
                parameter:(id)parameter
                 formData:(FormDataBlock)uploadData
                 progress:(ProgressBlock)uploadProgress
             completation:(SuccessBlock)success
                  failure:(FailureBlock)failure {
    
    [self setNetworkActivityIndicator:YES];
    
    [AFNet postUploadURL:url parameters:parameter formData:^(id<AFMultipartFormData> formData) {
        
        if (uploadData) {
            uploadData(formData);
        }
        
    } progress:^(NSProgress *progress) {
        
        float pro = (float)progress.completedUnitCount / (float)progress.totalUnitCount;
        
        DEBUGLog(@"上传进度:%f",pro);
        if (uploadProgress) {
            uploadProgress(progress);
        }
        
    } completation:^(id object) {
        
        if (success) {
            success(object);
        }
        
        [self setNetworkActivityIndicator:NO];

    } failure:^(NSError *error) {
        
        if (failure) {
            failure(error);
        }
    }];
    
}

+ (void)downloadWithURL:(NSString *)url
               progress:(ProgressBlock)downloadProgress
           completation:(SuccessBlock)success
                failure:(FailureBlock)failure {
    
    [AFNet downloadWithRequestURL:url progress:^(NSProgress *progress) {
        
        float pro = (float)progress.completedUnitCount / (float)progress.totalUnitCount;
        
        DEBUGLog(@"下载进度:%f",pro);
        if (downloadProgress) {
            downloadProgress(progress);
        }
        
    } completation:^(NSURLResponse *response, NSURL *filePath, NSError *error) {
        
        if (error == nil) {
            if (success) {
                success(filePath);
            }
        }else {
            if (failure) {
                failure(error);
            };
        }
        
    }];
}

//网络加载提示框
+ (void)showProgressView:(UIView *)view {
    
    netProgressView = [[NetProgressView alloc]init];
    [view addSubview:netProgressView];
    
}
+ (void)closeProgressViewFromView:(UIView *)view {
    if (netProgressView) {
        [netProgressView removeFromSuperview];
        netProgressView = nil;
    }
}
//错误提示
+ (void)errorView:(UIView *)view {
    
    alertView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 220, 50)];
    [view addSubview:alertView];
    alertView.backgroundColor = [UIColor whiteColor];
    alertView.layer.shadowColor = [UIColor blackColor].CGColor;
    alertView.layer.shadowOffset = CGSizeMake(3, 3);
    alertView.layer.shadowOpacity = 0.8;
    alertView.layer.shadowRadius = 3;
    
    alertView.alpha = 0;
    CGPoint center = view.center;
    center.y = center.y - 80;
    alertView.center = center;
    
    UILabel *alertLabel = [UILabel labelWithText:@"请检查网络~o_o~" fontSize:15 frame:CGRectMake(30, 0, 180, 50) color:[UIColor blackColor] textAlignment:1];
    [alertView addSubview:alertLabel];
    
    UIButton *closeBtn = [UIButton buttonWithType:UIButtonTypeContactAdd];
    [alertView addSubview:closeBtn];
    closeBtn.frame = CGRectMake(10, 10, alertView.frame.size.height - 20, alertView.frame.size.height - 20);
    closeBtn.tintColor = [UIColor blackColor];
    closeBtn.transform = CGAffineTransformMakeRotation(M_PI_4);
    [closeBtn addTarget:self action:@selector(closeAlert) forControlEvents:UIControlEventTouchUpInside];
    
    [UIView animateWithDuration:1.0 animations:^{
        
        alertView.alpha = 1.0;
        
    }];

}

+ (void)closeAlert {
    [alertView removeFromSuperview];
}

//网络活动指示
+ (void)setNetworkActivityIndicator:(BOOL)sign {
    UIApplication *app = [UIApplication sharedApplication];
    [app setNetworkActivityIndicatorVisible:sign];
}

@end
